<?php


namespace Biz\service\Store;

use Biz\model\store\VirtualPhoneRecord;
use Doraemon\tools\AlibabaCloud\VirtualPhone as AliVirtualPhone;
use Doraemon\tools\Log;

class VirtualPhone
{
    const EXPIRE_DAYS = 3;

    const POOL_KEY = 'FC100000141700256';

    const DIRECTION_UXO = 1;

    const DIRECTION_OXU = 2;


    //获取处理有效期的方法,目前确定都是7天
    public static function getVirtualExpireTime()
    {
        //获取当前时间
        $now_time = time();
        //7天后过期的时间
        $expire_time = $now_time + (86400 * self::EXPIRE_DAYS);
        $expire_date = date("Y-m-d H:i:s", $expire_time);
        return $expire_date;
    }

    //处理生成门店和用户的虚拟手机号
    public static function createVirtualPhone($official_phone, $user_phone, $store_id, $user_uid, $pool_key, $direction)
    {
        //首先判断双方是否已经有虚拟号码了
        $virtual_record_info = VirtualPhoneRecord::getRecordInfo($user_phone, $official_phone, $direction);
        //用来标识两者的虚拟手机号是否有效;
        $is_effect = 0;
        if ($virtual_record_info) {
            //如果有的话,根据过期时间判断该虚拟号是否过期
            $record_expire_date = $virtual_record_info[0]['virtual_expire_time'];
            //获取当前时间
            $now_time = time();
            $record_expire_time = strtotime($record_expire_date);
            if ($record_expire_time > $now_time) {
                $is_effect = 1;
            }
        }
        //根据标识进行不同的处理
        if ($is_effect) {
            //如果还有效的话,直接返回数据库中的虚拟手机号
            $virtual_record_phone = $virtual_record_info[0]['virtual_phone'];
            $virtual_record_expire_time = $virtual_record_info[0]['virtual_expire_time'];
            $data = [
                "virtual_phone" => $virtual_record_phone,
                "virtual_expire_time" => $virtual_record_expire_time,
            ];
        } else {
            //如果没有的话,去调用阿里云服务生成虚拟号
            //先获取过期时间
            $expire_time = self::getVirtualExpireTime();
            $params = [
                self::DIRECTION_UXO => [$user_phone, $official_phone, $expire_time, $pool_key,'',$user_uid,$store_id],
                self::DIRECTION_OXU => [$official_phone, $user_phone, $expire_time, $pool_key,'',$user_uid,$store_id],
            ];
            $virtual_info = AliVirtualPhone::Virtual(...$params[$direction]);
            $data = [];
            if ($virtual_info && !empty($virtual_info)) {
                $virtual_phone = $virtual_info['SecretBindDTO']['SecretNo'];
                $subs_id = $virtual_info['SecretBindDTO']['SubsId'];
                //将生成的信息保存值数据库中
                $virtual_data = [
                    "user_uid" => $user_uid,
                    "store_id" => $store_id,
                    "user_phone" => $user_phone,
                    "official_phone" => $official_phone,
                    "virtual_phone" => $virtual_phone,
                    "virtual_expire_time" => $expire_time,
                    "subs_id" => $subs_id,
                    "pool_key" => $pool_key,
                    "direction" => $direction,
                ];
                $result = VirtualPhoneRecord::create($virtual_data);
                //拼接返回的数据
                $data = [
                    "virtual_phone" => $virtual_phone,
                    "virtual_expire_time" => $expire_time,
                ];

            } else {
                //记录失败日志,便于查找问题
                Log::notice('存在虚拟手机号生成失败', [
                    'store_id' => $store_id,
                    'user_uid' => $user_uid,
                    "user_phone" => $user_phone,
                    "official_phone" => $official_phone,
                ]);
            }
        }
        return $data;
    }


    public static function unbindVirtualPhone($virtual_phone, $subs_id){
        return AliVirtualPhone::unbindVirtualPhone($virtual_phone, $subs_id, self::POOL_KEY);
    }


}